package com.wjk.kylin.mall.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjk.kylin.id.core.uid.UidGenerator;
import com.wjk.kylin.lock.annotation.KylinLock;
import com.wjk.kylin.mall.common.core.bo.member.MemberUserNameBO;
import com.wjk.kylin.mall.common.core.constant.Constants;
import com.wjk.kylin.mall.common.core.dto.base.BaseOutDTO;
import com.wjk.kylin.mall.common.core.dto.base.DataIdInDTO;
import com.wjk.kylin.mall.common.core.dto.base.DataOutDTO;
import com.wjk.kylin.mall.common.core.dto.member.MemberUserNameInDTO;
import com.wjk.kylin.mall.common.redis.utils.RedisUtil;
import com.wjk.kylin.mall.member.beanmapper.MemberEntityMapper;
import com.wjk.kylin.mall.member.bo.MemberBO;
import com.wjk.kylin.mall.member.dto.MemberInDTO;
import com.wjk.kylin.mall.member.entity.MemberEntity;
import com.wjk.kylin.mall.member.mapper.MemberMapper;
import com.wjk.kylin.mall.member.service.IMemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


/**
 * @Author: wangjinkui
 * @Date: 2021/5/28
 * @Description : 会员信息
 */
@Slf4j
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, MemberEntity> implements IMemberService {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    @Qualifier("cachedUidGenerator")
    private UidGenerator uidGenerator;

    /**
     * 根据id查询数据
     *
     * @param inDTO
     * @return
     */
    @Override
    public BaseOutDTO getById(DataIdInDTO inDTO) {
        DataOutDTO<MemberBO> outDTO = new DataOutDTO<>();
        outDTO.setData(MemberEntityMapper.INSTANCE.entity2BO(baseMapper.selectById(inDTO.getId())));
        return outDTO;
    }

    @Override
    @KylinLock(keys = "#inDTO.username")
    @Transactional
    public BaseOutDTO add(MemberInDTO inDTO) {
        MemberEntity entity = MemberEntityMapper.INSTANCE.dto2Entity(inDTO);
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        entity.setId(uidGenerator.getUID());
        //TODO 初始化密码
        entity.setPassword(passwordEncoder.encode(Constants.INIT_PASSWORD));

        if (super.save(entity)) {
            return BaseOutDTO.success();
        }
        return BaseOutDTO.error("操作失败，请重试！");
    }

    @Override
    public BaseOutDTO loadUserByUsername(MemberUserNameInDTO inDTO) {
        DataOutDTO<MemberUserNameBO> outDTO = new DataOutDTO<>();

        QueryWrapper<MemberEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("username", inDTO.getUsername());
        wrapper.select("id", "username", "nickname", "password", "status");

        outDTO.setData(MemberEntityMapper.INSTANCE.entity2UserNameBO(this.getOne(wrapper)));
        return outDTO;
    }

}
